#!/bin/sh

#
# Check different makes
#
# Copyright 2023 and 2024 Odin Kroeger.
#
# This file is part of suCGI.
#
# suCGI is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# suCGI is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
# Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with suCGI. If not, see <https://www.gnu.org/licenses/>.
#

# shellcheck disable=2015


#
# Defaults
#

# Makes to test
makes='make bmake bsdmake gmake remake'

# Be quiet?
quiet=

# Be verbose?
verbose=


#
# Initialiation
#

set -Cefu
scriptsdir="$(cd -P "$(dirname -- "$0")" && pwd)"
: "${scriptsdir:?}"
readonly scriptsdir
srcdir="$(dirname -- "$scriptsdir")"
: "${srcdir:?}"
readonly srcdir
# shellcheck disable=1091
. "$scriptsdir/../libutil.sh"	|| exit
init				|| exit
tmpdir tmp "$srcdir"		|| exit


#
# Constants
#

# Current working directory
wd="$(pwd)" || exit
: "${wd:?}"
readonly wd


#
# Options
#

OPTIND=1 OPTARG='' opt=''
# shellcheck disable=2034
while getopts dc:hqv opt; do
	# shellcheck disable=2154
	case $opt in
	(h) exec cat <<EOF ;;
$progname - test different makes

Usage:     $progname [-c cc] [-d] [-q] [-v] [make ...]
           $progname -h

Options:
    -c cc  Use cc as C compiler.
    -d     Enable debugging mode.
    -q     Be quieter.
    -v     Show, rather than log, test output.
    -h     Show this help screen.
EOF
	(d) set -x ;;
	(c) CC="$OPTARG" ;;
	(q) quiet=y ;;
	(v) verbose=y ;;
	(*) exit 2
	esac
done
shift $((OPTIND - 1))
unset opt

# shellcheck disable=2086
[ $# -eq 0 ] && set -- $makes

# Use a fast compiler, if one is available
if ! [ "${CC-}" ]
then
	for cc in c99 cc tcc clang
	do
		if command -v "$cc" >/dev/null
		then
			CC="$cc"
			break
		fi
	done
fi
[ "${CC-}" ] && export CC


#
# Source tree
#

warn -q 'Copying source tree ...'
cd "$srcdir" || exit
logged -d"$wd" make distar="$TMPDIR/dist.tgz" dist=dist "$TMPDIR/dist.tgz"
cd "$TMPDIR" || exit
tar xzf dist.tgz
cd dist || exit
cp config.h.tpl config.h
warn -q 'Configuring build ...'
logged -d"$wd" ./configure


#
# Main
#

nmakes=0 failures=
for make
do
	absfname="$(command -v "$make" 2>/dev/null)" || continue
	i=0
	while [ "$i" -lt "$nmakes" ]
	do
		eval seenmake="\"\${make_$i-}\""
		# shellcheck disable=2154
		if cmp "$absfname" "$seenmake" >/dev/null 2>&1
		then
			warn '%s has already been checked as %s' \
			     "$make" "${seenmake##*/}"
			continue 2
		fi
		i="$((i + 1))"
	done
	eval "make_$nmakes"='$absfname'
	nmakes="$((nmakes + 1))"

	warn 'Checking %s ... ' "$make"
	logged -d"$wd" "$make" mrproper && 
		logged -d"$wd" "$make" all ||
		failures="$failures$make, "
done

if [ "$failures" ]
then err -s70 'Failures: %s' "${failures%, }"
else warn -q 'All makes passed'
fi

